#include <stdlib.h>
#include <stdio.h>
#include "../../plugins.h"
#include "../../gui_al.h"
#include "search_tab.h"
#include "../../gui_io.h"
#include "../../callbacks.h"
#include "../../settings.h"
#include "../../tabs.h"


#define TAB_NAME "search_tab.so"

/*  Compiling this file:
 *  gcc -shared -o modules/plgn_example.so -L. -lejourn modules/plgn_example.c
 *  The -shared is to make it loadable dynamically, and -L. -lejourn is to tell
 *  it to dynamically load the ejourn libraries.
 */

GtkWidget *srch_page;



 /* init(gui) is called when the plugin is loaded.
  * gui will be NULL;
  */
void  ___init(void *ignoreme) 
{
	printf("Search got tab number:%i\n", elog_tab_register(TAB_NAME));

	GtkWidget *srch_lbl;
    GtkWidget *hpaned2 = elog_srch_initialize();
    gtk_container_add(GTK_CONTAINER(_notebook), hpaned2);
    srch_lbl = gtk_label_new(_("Search"));
    //gtk_wiget_show (srch_lbl);
    gtk_notebook_set_tab_label(GTK_NOTEBOOK(_notebook),
                               gtk_notebook_get_nth_page(GTK_NOTEBOOK
													 (_notebook),
                                                     elog_tab_num(TAB_NAME)),
							   srch_lbl);
	gtk_widget_show_all(hpaned2);
	gtk_widget_show(srch_lbl);
	srch_page = hpaned2;

}
 
 /* end(void) is called when the program unloads your plugin.  Please clean
  * everything it's done up:  Including what it's added to the gui!
  * HINT:  You should have stored the gtk widgets you needed to delete them
  * later.
  */
void ___end(void *ignoreme)
{
	 //gtk_widget_destroy(srch_page);
	gtk_notebook_remove_page(GTK_NOTEBOOK(_notebook), elog_tab_num(TAB_NAME));
	elog_tab_unregister(elog_tab_num(TAB_NAME));
}
 /* ugly(void) is called when the program exits but your plugin is still loaded.
  * It means you don't need to bother cleaning up memory really:  the kernel should
  * handle that for you and we just want that derned window to get off the users
  * desk.  So clean up what you need, save what you love, and finish it quickly if 
  * you can.
  * HINT:  A few Global state variables won't destroy the world here ;). 
  */
void ___ugly(void *ignoreme)
{
 
}
 

void ___search(struct elog_plgn_data_srch *search)
{
	traverSearch(search->dir, search->term, search->functor);
	elog_gui_al_page(elog_tab_num(TAB_NAME));
}

/* char *info returns user-level information about what the plugin does.
 */
char *___info()
{
  return "Search Page -- This is a search view for your journal.";

} 
 
/* char *author returns the list of authors, please insert a \n after
 * each author for display purposes.
 */
char *___author()
{
  return "Search Page by Chris <MA_D> Hilton";
}


void ___focus(void *ignoreme)
{
	int n = elog_set_get_int("SEARCH_DIVIDE_2");
	if (n <= 0)
		n = elog_gui_al_width() - 128;
	elog_gui_al_srch_pane(n);
	elog_gui_al_menu_detach_view();
	elog_tab_srch_srch();
	elog_gui_io_save();
}

void ___cut(void *ignoreme)
{
	elog_tab_srch_cut();
}

void ___copy(void *ignoreme)
{
	elog_tab_srch_copy();
}
void ___paste(void *ignoreme)
{
	elog_tab_srch_paste();
}
/*
 * This is the little menubar search function.  Not the main search.
 */
void ___srch(void *ignoreme)
{
	elog_tab_srch_srch();
}
void ___res(int *num)
{
	int n = *num;
	elog_tab_srch_res(n);
}
